//
//  SeleController.m
//  Topic1
//
//  Created by 孙添凤 on 16/12/6.
//  Copyright © 2016年 孙添凤. All rights reserved.
//

#import "SeleController.h"
#import "OneSelectCell.h"
#import "PersonModel.h"
@interface SeleController (){
    UITableView *tbView;
}
@property(nonatomic,strong)NSMutableArray *mutArr;
@property(nonatomic,copy)NSString *dic_id;
@property(nonatomic,copy)NSString *dic_name;
@property(nonatomic,strong)NSMutableDictionary *mmDic;
@property(nonatomic,strong)NSMutableDictionary *nameDic;
@end

@implementation SeleController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _mutArr =[NSMutableArray array];
    _mmDic =[NSMutableDictionary dictionary];
    _nameDic =[NSMutableDictionary dictionary];
    self.title = @"选择";
    self.view.backgroundColor =[UIColor whiteColor];
    
    tbView =[[UITableView alloc]init];
    [self.view addSubview:tbView];
    [tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    tbView.delegate = self;
    tbView.dataSource = self;
    UIButton *RightNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    RightNavBtn.frame = CGRectMake(0, 0, 80, 30);
    [RightNavBtn setTitle:@"确定" forState:UIControlStateNormal];
    [RightNavBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [RightNavBtn addTarget:self action:@selector(RightClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:RightNavBtn];
    self.navigationItem.rightBarButtonItem = item;
    if(_isMore){
     [self dataRequest];
    }
    [self createLeftNavBar];

    
}

-(void)RightClick:(UIButton *)btn{

    NSMutableDictionary *dd = [NSMutableDictionary dictionary];
    NSMutableArray *mmm =[NSMutableArray array];
    NSMutableArray *cccc =[NSMutableArray array];
    if(!_isMore){
    [dd setObject:_dic_id forKey:_montageName];
    _abc(dd,nil,_dic_name);
    }else{
       NSArray *keys = [_mmDic allKeys];
        NSArray *namekeys = [_nameDic allKeys];
        for(NSInteger i=0;i<keys.count;i++){
            NSDictionary *ddd =[NSDictionary dictionaryWithObject:[_mmDic objectForKey:[NSString stringWithFormat:@"%@",keys[i]]] forKey:_montageName1];
            [mmm addObject:ddd];
        }
        for(NSInteger i=0;i<namekeys.count;i++){
            NSString *ddd =[_nameDic objectForKey:namekeys[i]];
            [cccc addObject:ddd];
        }
        NSString *str = [cccc componentsJoinedByString:@","];
        _abc(nil,mmm,str);
    }
    [self.navigationController popViewControllerAnimated:YES];

}

-(void)dataRequest{
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    [params setObject:@"448" forKey:@"tableId"];
    [params setObject:@"7688" forKey:@"pageId"];
    NSString *requestUrl =[NSString stringWithFormat:@"%@%@",BASE_URL,GOTOSCHOOL];
    
    _dicArr = [[NSMutableArray alloc]init];
    
    [NetRequest requestUrl:requestUrl andParamas:params andReturnBlock:^(NSData *data, NSError *error) {
        if(data!=nil&&!error){
            NSDictionary *obj =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            
            NSArray *arr = [obj objectForKey:@"rows"];
            for(NSDictionary *dic in arr){
               PersonModel *model =[[PersonModel alloc]init];
                model.name = [dic objectForKey:@"AFM_1"];
                model.LMF_ID = [dic objectForKey:@"LMF_ID"];
                model.isSele = NO;
                [_dicArr addObject:model];
            }
            //_dicArr = aaaa;
            [tbView reloadData];
        }else{
            
        }
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dicArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str = @"str";
    OneSelectCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
    if(cell==nil){
        cell=[[OneSelectCell alloc]initIdentifier:str];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    if(_isMore){
        PersonModel *model = [_dicArr objectAtIndex:indexPath.row];
        cell.la.text = model.name;
        
        if(model.isSele){
            [_mmDic setObject:model.LMF_ID forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
            
            [cell.btn setImage:[UIImage imageNamed:@"形状-20-拷贝"] forState:UIControlStateNormal];
            model.isSele = YES;
        }else{
            [_mmDic removeObjectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
            [cell.btn setImage:[UIImage imageNamed:@"椭圆-1-拷贝-3"] forState:UIControlStateNormal];
            model.isSele = NO;
        }

        
    }else{
        NSDictionary *dic = [_dicArr objectAtIndex:indexPath.row];
        cell.la.text = [dic objectForKey:@"DIC_NAME"];
        [_mutArr addObject:cell.btn];
    }
    
//    PersonModel *model = _dicArr[indexPath.row];
    
    return cell;
    
}


-(void)createLeftNavBar{
    UIButton *leftNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftNavBtn.frame = CGRectMake(0, 0, 11, 37/2);
    [leftNavBtn setBackgroundImage:[UIImage imageNamed:@"形状1-19-拷贝-4"] forState:UIControlStateNormal];
    [leftNavBtn addTarget:self action:@selector(LeftClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:leftNavBtn];
    self.navigationItem.leftBarButtonItem = item;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OneSelectCell *cell =[tableView cellForRowAtIndexPath:indexPath];
    if(!_isMore){
        _dic_id =[NSString stringWithFormat:@"%@",[[_dicArr objectAtIndex:indexPath.row] objectForKey:@"DIC_ID"]];
        _dic_name = [[_dicArr objectAtIndex:indexPath.row] objectForKey:@"DIC_NAME"];
        for(int i=0;i<_mutArr.count;i++){
            UIButton *btn= _mutArr[i];
            if(i==indexPath.row){
              [btn setImage:[UIImage imageNamed:@"形状-20-拷贝"] forState:UIControlStateNormal];
            }else{
                [btn setImage:[UIImage imageNamed:@"椭圆-1-拷贝-3"] forState:UIControlStateNormal];
            }
        }
    }else{
        PersonModel *model =[_dicArr objectAtIndex:indexPath.row];
        if(!model.isSele){
            [_mmDic setObject:model.LMF_ID forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
            [_nameDic setObject:model.name forKey:[NSString stringWithFormat:@"name%ld",(long)indexPath.row]];
            
                    [cell.btn setImage:[UIImage imageNamed:@"形状-20-拷贝"] forState:UIControlStateNormal];
//                model.isSele = YES;
        }else{
            [_mmDic removeObjectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
            [_nameDic removeObjectForKey:[NSString stringWithFormat:@"name%ld",(long)indexPath.row]];
                                [cell.btn setImage:[UIImage imageNamed:@"椭圆-1-拷贝-3"] forState:UIControlStateNormal];
//            model.isSele = NO;
        }
        
//    }
    
    model.isSele = !model.isSele;

}
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
